﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace FYPDemo
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                bindDDLCategory();
                string supplierCode = Request.QueryString["SupplierCode"];
                string ID = Request.QueryString["ID"];
               
                DataTable dt = ProductManagement.DisplayPictureForVendor(ID);

                if (dt.Rows.Count > 0)
                {
                    txtProductID.Text = dt.Rows[0][1].ToString();
                    txtProductName.Text = dt.Rows[0][2].ToString();
                    txtProductPrice.Text = dt.Rows[0][3].ToString();
                    prodImage.ImageUrl = dt.Rows[0][4].ToString();
                    txtQuantity.Text = dt.Rows[0][7].ToString();
                    txtSafeLevel.Text = dt.Rows[0][9].ToString();
                    txtCost.Text = dt.Rows[0][10].ToString();
                    txtDesc.Text = dt.Rows[0][11].ToString();
                }
            }

        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            enabledTrue();
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            int result = 0;
            int ID = Convert.ToInt32(txtProductID.Text);
            string Description = txtDesc.Text;
            string ProductName = txtProductName.Text;
            decimal productPrice = Convert.ToDecimal(txtProductPrice.Text);
            string Category = ddlCategory.SelectedItem.ToString();
            int increaseQuantity = Convert.ToInt32(txtIncreaseQuantity.Text);
            string imgPath = prodImage.ImageUrl.ToString();
            string uploadTo = "Product/" + Category;
            int safeLevel = Convert.ToInt32(txtSafeLevel.Text);
            decimal cost = Convert.ToDecimal(txtCost.Text);


            if (imgPath != "" && ProductName != "" && Category != "")
            {
                System.IO.File.Delete(Server.MapPath(imgPath));
                HttpPostedFile theFile = Session["PostedFile"] as HttpPostedFile;
                uploadFile(theFile, uploadTo, ProductName);
                prodImage.ImageUrl = "~/" + uploadTo + "/" + ProductName + ".jpg";
                imgPath = prodImage.ImageUrl.ToString();
            }

            result = ProductManagement.updateVendorProduct(ProductName, ID, productPrice, imgPath, Category, increaseQuantity, safeLevel, cost, Description);

            int TempQuantity = Convert.ToInt32(txtQuantity.Text);
            int total = TempQuantity + increaseQuantity;
            txtQuantity.Text = total.ToString();
            enabledFalse();
        }

        protected void bindDDLCategory()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();

            conndb db = new conndb();
            SqlConnection con = db.GetOpenConnection();
            SqlCommand cmd = con.CreateCommand();

            try
            {
                cmd.CommandText = "listProductCategory";
                cmd.CommandType = CommandType.StoredProcedure;

                da.SelectCommand = cmd;
                da.Fill(dt);

                ddlCategory.DataSource = dt;
                ddlCategory.DataTextField = "Value";
                ddlCategory.DataValueField = "CategoryID";
                ddlCategory.DataBind();
            }

            catch (Exception ex)
            {
                con.Close();
                throw ex;
            }

            finally
            {
                con.Close();
            }

        }
        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));
            }
        }

        private void enabledTrue()
        {
            txtProductName.Enabled = true;
            txtProductPrice.Enabled = true;
            txtIncreaseQuantity.Enabled = true;
            btnUpload.Enabled = true;
            ImgUpload.Enabled = true;
            ddlCategory.Enabled = true;
            txtCost.Enabled = true;
            txtSafeLevel.Enabled = true;
            txtDesc.Enabled = true;
        }

        private void enabledFalse()
        {
            txtProductName.Enabled = false;
            txtProductPrice.Enabled = false;
            txtIncreaseQuantity.Enabled = false;
            btnUpload.Enabled = false;
            ImgUpload.Enabled = false;
            ddlCategory.Enabled = false;
            ddlCategory.Enabled = false;
            txtCost.Enabled = false;
            txtSafeLevel.Enabled = false;
            txtDesc.Enabled = false;
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string prodName = "TempProduct";
            string uploadTo = "Product";

            if (ImgUpload.HasFile)
            {
                if (Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + prodName)))
                {
                    System.IO.File.Delete(Server.MapPath("~/" + uploadTo + "/" + prodName));
                }

                else
                {
                    HttpPostedFile theFile = ImgUpload.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, prodName);
                    prodImage.ImageUrl = "~/" + uploadTo + "/" + prodName + ".jpg";
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string ID = Request.QueryString["ID"];

            conndb db = new conndb();
            SqlConnection con = db.GetOpenConnection();
            SqlCommand cmd = con.CreateCommand();

            int tempID = Convert.ToInt32(ID);

            ProductManagement.DeleteProduct(tempID);
            Response.Redirect("DisplayProductForVendor.aspx");
        }
    }
}